package com.dreamchaser.familydevice.shiro.realm;

import com.dreamchaser.familydevice.util.Encode.MD5Util;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.credential.CredentialsMatcher;

/***
 *
 *
 * @Author: SJie
 * Date: 2020/2/18 下午2:00
 * Description:
 *
 */
// 自定义认证通过方式
public class CustomCredentialsMatcher implements CredentialsMatcher {


    @Override
    public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {

        // 黑白名单
//        JwtToken jwtToken = (JwtToken) authenticationToken;
//        String ip = jwtToken.getIpHost();
//        // 认证 密码是否匹配
//        // 取消记住密码，取消session 所以set集合中只会有一个
        String account = authenticationInfo.getPrincipals().getRealmNames().iterator().next();
        String password =authenticationInfo.getPrincipals().getPrimaryPrincipal().toString();
        String key = authenticationInfo.getCredentials().toString();
        if (!key.equals(MD5Util.charToMd5(account+password+account))) {
            throw new IncorrectCredentialsException("密码已修改，请重新登陆");
        }
        return true;
    }
}
